package com.ruoyi.web.controller.prevention;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.PreventionDeviceVO;
import com.ruoyi.system.entity.PreventionDevice;
import com.ruoyi.system.entity.PreventionPlan;
import com.ruoyi.system.service.PreventionDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 双重预防-装置管理(PreventionDevice)表控制层
 *
 * @author makejava
 * @since 2022-11-17 11:18:35
 */
@RestController
@Api("装置管理接口")
@RequestMapping("/preventionDevice")
public class PreventionDeviceController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PreventionDeviceService preventionDeviceService;

    /**
     * 分页查询
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(PreventionDevice preventionDevice) {
        startPage();
        List<PreventionDeviceVO> preventionDeviceVOS = preventionDeviceService.queryByPage(preventionDevice);
        return getDataTable(preventionDeviceVOS);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.preventionDeviceService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param preventionDevice 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermi('preventionDevice:create')")
    public AjaxResult add(@RequestBody PreventionDevice preventionDevice) {
        return AjaxResult.success(this.preventionDeviceService.insert(preventionDevice));
    }

    /**
     * 编辑数据
     *
     * @param preventionDevice 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('preventionDevice:update')")
    public AjaxResult edit(@RequestBody PreventionDevice preventionDevice) {
        return AjaxResult.success(this.preventionDeviceService.update(preventionDevice));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    @PreAuthorize("@ss.hasPermi('preventionDevice:delete')")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.preventionDeviceService.deleteById(id));
    }


    @GetMapping("/listByLevel")
    @ApiOperation("装置管理列表")
    public AjaxResult list() {
        List<PreventionDevice> preventionDevices = preventionDeviceService.listByLevel();
        return AjaxResult.success(preventionDevices);
    }

    @GetMapping("/countByLevel")
    public AjaxResult countByLevel() {
        List<Map<String,Integer>> preventionDevices = preventionDeviceService.countByLevel();
        return AjaxResult.success(preventionDevices);
    }


    /**
     * 导出
     * @param response
     * @param preventionDevice
     */
    @Log(title = "排查计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreventionDevice preventionDevice) {
        List<PreventionDevice> list = preventionDeviceService.queryList(preventionDevice);
        ExcelUtil<PreventionDevice> util = new ExcelUtil<>(PreventionDevice.class);
        util.exportExcel(response, list, "排查计划");
    }





}

